<?php

namespace app\store\model;

use app\common\model\Archives as ArchivesModel;

/**
 * 档案管理
 * Class Article
 * @package app\store\model
 */
class Archives extends ArchivesModel
{
    /**
     * 获取档案列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $store_user_id=$_SESSION['yoshop_store']['user']['store_user_id'];
        $where =[];
        if($store_user_id!=10001){
            $where = ['user_id'=>$store_user_id];
        }
        return $this
            ->with(['store'])
            ->where($where)
            ->where('is_delete',0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if(!preg_match("/^\d*$/",$data['age'])||$data['age']>=150){
            $this->error = '请填写正确年龄';
            return false;
        }
        if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) {
            $this->error = '请填写正确手机号';
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        $data['user_id'] = $_SESSION['yoshop_store']['user']['store_user_id'];
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if(!preg_match("/^\d*$/",$data['age'])||$data['age']>=150){
            $this->error = '请填写正确年龄';
            return false;
        }
        if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) {
            $this->error = '请填写正确手机号';
            return false;
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
    public static function detail($id)
    {
        return static::get($id);
    }
}